package com.penghaisoft.microwms.producthand.web.controller.outwarehouse;

import com.penghaisoft.microwms.common.Constant;
import com.penghaisoft.microwms.common.base.model.BaseDictionary;
import com.penghaisoft.microwms.common.controller.BaseController;
import com.penghaisoft.microwms.common.data.model.ProScrapOut;
import com.penghaisoft.microwms.common.dto.*;
import com.penghaisoft.microwms.product.service.IBaseDictionaryService;
import com.penghaisoft.microwms.product.service.IProScrapOutService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @version 1.0
 * @Description 成品报废出库
 * copy@luoteng wzhg
 * @date 2018年6月18日
 */
@RestController
@RequestMapping(value = "/pro/scrap/outwh")
public class ProScrapOutWhController extends BaseController {

    @Autowired
    private IBaseDictionaryService baseDictionaryService;

    @Resource
    private StringRedisTemplate stringRedisTemplate;//注入对象

    @Autowired
    private IProScrapOutService proScrapOutService;

    /**
     * @param proScrapOut
     * @return HandResp
     * @Title confirm
     * @Description 提单
     * @copy luoteng @edit wzhg
     * @date 2018年6月14日:上午10:15:19
     */
    @PostMapping("loadBill")
    public HandResp confirm(@RequestBody ProScrapOut proScrapOut) {
        HandResp result = new HandResp();
        Resp resp = proScrapOutService.loadBill(proScrapOut);
        result.setCode(resp.getCode());
        result.setMsg(resp.getMsg());
        return result;
    }

    /**
     * @param proScrapOut
     * @return HandResp
     * @Title info
     * @Description 详情
     * @copy luoteng @edit wzhg
     * @date 2018年6月14日:上午10:55:29
     */
    @PostMapping("info")
    public HandResp info(@RequestBody ProScrapOut proScrapOut) {
        HandResp result = new HandResp();

        proScrapOut.setActiveFlag("1");
        List<ProScrapOut> list = proScrapOutService.queryListByAny(proScrapOut);

        // 字典表获取型号类型的中文名
        BaseDictionary baseDictionary1 = new BaseDictionary();
        baseDictionary1.setActiveFlag("1");
        baseDictionary1.setDicType(Constant.MODEL_TYPE_P);
        List<BaseDictionary> list1 = baseDictionaryService.queryAllByCondition(baseDictionary1);

        if (list != null && !list.isEmpty()) {
            for (ProScrapOut tmpData : list) {
                if (list1 != null && !list1.isEmpty()) {
                    for (BaseDictionary tmp : list1) {
                        if (tmp.getDicCode().equals(tmpData.getModelType())) {
                            tmpData.setModelTypeCn(tmp.getDicName());
                        }
                    }
                }
            }
        }

        result.setCode(Constant.SUCCESS);
        result.setList(list);
        return result;
    }

    /**
     * @param proScrapOutDto
     * @return HandResp
     * @Title scan
     * @Description 扫码
     * @copy luoteng @edit wzhg
     * @date 2018年6月14日:上午11:08:57
     */
    @PostMapping("scan")
    public HandResp scan(@RequestBody ProScrapOutDto proScrapOutDto) {
        HandResp resp = new HandResp();
        String key = Constant.PRO_SCRAPOUT_PREFIX + proScrapOutDto.getBarcode();

        if (stringRedisTemplate.hasKey(key)) {
            resp.setCode(Constant.FAIL);
            resp.setMsg("当前条码正在处理中，请不要重复扫描！");
            return resp;
        } else {
            stringRedisTemplate.opsForValue().set(key, "1", Constant.PRO_HAND_LOST_MINUTES, TimeUnit.MINUTES);//写入对象，并设置失效时间

            TransResult result = new TransResult();
            try {
                result = proScrapOutService.scrapOutScanBarcode(proScrapOutDto);
                resp.setCode(result.getCode());
                resp.setMsg(result.getMsg());
                resp.setList(result.getList());
            } catch (RuntimeException e) {
                resp.setCode(Constant.FAIL);
                resp.setMsg(e.getMessage());
                stringRedisTemplate.delete(key);//删除对象
            }
        }

        stringRedisTemplate.delete(key);//删除对象
        return resp;
    }

    /**
     * @param proScrapOutDto
     * @return HandResp
     * @Title del
     * @Description 删除条码
     * @copy luoteng @edit wzhg
     * @date 2018年6月14日:下午5:10:45
     */
    @PostMapping("del")
    public HandResp del(@RequestBody ProScrapOutDto proScrapOutDto) {
        HandResp resp = new HandResp();
        String key = Constant.PRO_SCRAPOUT_PREFIX + proScrapOutDto.getBarcode();

        if (stringRedisTemplate.hasKey(key)) {
            resp.setCode(Constant.FAIL);
            resp.setMsg("当前条码正在处理中，请不要重复扫描！");
            return resp;
        } else {
            stringRedisTemplate.opsForValue().set(key, "1", Constant.PRO_HAND_LOST_MINUTES, TimeUnit.MINUTES);//写入对象，并设置失效时间

            TransResult result = new TransResult();
            try {
                result = proScrapOutService.scrapOutDelBarcode(proScrapOutDto);
                resp.setCode(result.getCode());
                resp.setMsg(result.getMsg());
                resp.setList(result.getList());
            } catch (RuntimeException e) {
                resp.setCode(Constant.FAIL);
                resp.setMsg(e.getMessage());
                stringRedisTemplate.delete(key);//删除对象
            }
        }

        stringRedisTemplate.delete(key);//删除对象
        return resp;
    }

    /**
     * @param proScrapOutDto
     * @return HandResp
     * @Title complete
     * @Description 确认完成
     * @copy luoteng @edit wzhg
     * @date 2018年6月14日:下午5:54:53
     */
    @PostMapping("complete")
    public HandResp complete(@RequestBody ProScrapOutDto proScrapOutDto) {
        HandResp resp = new HandResp();
        String key = Constant.PRO_SCRAPOUT_PREFIX + proScrapOutDto.getLoginName();

        if (stringRedisTemplate.hasKey(key)) {
            resp.setCode(Constant.FAIL);
            resp.setMsg("请等" + Constant.PRO_HAND_LOST_MINUTES + "分钟再操作！");
            return resp;
        } else {
            stringRedisTemplate.opsForValue().set(key, "1", Constant.PRO_HAND_LOST_MINUTES, TimeUnit.MINUTES);//写入对象，并设置失效时间

            TransResult result = new TransResult();
            try {
                result = proScrapOutService.scrapOutComplete(proScrapOutDto);
                resp.setCode(result.getCode());
                resp.setMsg(result.getMsg());
                resp.setList(result.getList());
            } catch (RuntimeException e) {
                resp.setCode(Constant.FAIL);
                resp.setMsg(e.getMessage());
                stringRedisTemplate.delete(key);//删除对象
            }
        }

        stringRedisTemplate.delete(key);//删除对象
        return resp;
    }
}
